/**
* @Title: ClientObject.java
* @Package com.an.objecttran
* @Description: TODO(用一句话描述该文件做什么)
* @author antch,861710865@qq.com
* @date 2017年7月26日 下午3:41:29
* @version V1.0
*/

package com.an.objecttran;

import java.util.Scanner;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;

/**
 * @ClassName: ClientObject
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @author antch,861710865@qq.com
 * @date 2017年7月26日 下午3:41:29
 *
 */

public class ClientObject {

	private String ip;

	private int port;

	public ClientObject(String ip, int port) {
		super();
		this.ip = ip;
		this.port = port;
	}

	public void run() {
		EventLoopGroup group = new NioEventLoopGroup();

		Bootstrap bootstrap = new Bootstrap();

		bootstrap.group(group);

		bootstrap.channel(NioSocketChannel.class);
		bootstrap.handler(new ClientObjectInitializer());
		Scanner scan = new Scanner(System.in);
		try {
			Channel channel = bootstrap.connect(ip, port).sync().channel();

			String str = null;
			while (true) {
				str = scan.nextLine();
				if ("1".equals(str)) {
					Dog dog = new Dog("小白", 7, "dog_xa");
					System.out.println("发送对象：：" + dog);
					channel.writeAndFlush(dog);
					System.out.println("服务器应答：");
				} else {
					break;
				}

			}
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			scan.close();
			e.printStackTrace();
		} finally {
			scan.close();
		}

	}

	public static void main(String[] args) {
		new ClientObject("localhost", 8080).run();
	}
}
